package com.rpay.service.banner.impl;

import com.rpay.common.dto.banner.BannerQueryDTO;
import com.rpay.common.dto.banner.BannerDTO;
import com.rpay.dao.banner.BannerDao;
import com.rpay.model.banner.BannerDO;
import com.rpay.model.banner.BannerQueryDO;
import com.rpay.service.banner.IBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BannerServiceImpl implements IBannerService {
    @Autowired
    private BannerDao bannerDao;

    @Override
    public BannerQueryDTO list(BannerQueryDTO bannerQueryDTO) {
        BannerQueryDO bannerSqlDO = convertSqlDTOToDO(bannerQueryDTO);
        List<BannerDO> bannerDOList = bannerDao.list(bannerSqlDO);
        List<BannerDTO> list = new ArrayList<>();
        if (bannerDOList != null && bannerDOList.size() > 0) {
            for (BannerDO bannerDO : bannerDOList) {
                BannerDTO bannerDTO = convertDOToDTO(bannerDO);
                list.add(bannerDTO);
            }
        }
        BannerQueryDTO data = convertSqlDOToDTO(list);
        return data;
    }

    @Override
    public boolean post(BannerDTO bannerDTO) {
        BannerDO bannerDO = convertDTOToDO(bannerDTO);
        bannerDao.post(bannerDO);
        return true;
    }

    @Override
    public boolean delete(Integer id) {
        bannerDao.delete(id);
        return true;
    }

    @Override
    public boolean patch(BannerDTO bannerDTO) {
        BannerDO bannerDO = convertDTOToDO(bannerDTO);
        bannerDao.patch(bannerDO);
        return true;
    }

    private BannerDO convertDTOToDO(BannerDTO bannerDTO) {
        BannerDO bannerDO = null;
        if (bannerDTO != null) {
            bannerDO = new BannerDO();
            bannerDO.setId(bannerDTO.getId());
            bannerDO.setUrl(bannerDTO.getUrl());
            bannerDO.setEnUrl(bannerDTO.getEnUrl());
            bannerDO.setTitle(bannerDTO.getTitle());
            bannerDO.setEnTitle(bannerDTO.getEnTitle());
            bannerDO.setLinkUrl(bannerDTO.getLinkUrl());
            bannerDO.setEnLinkUrl(bannerDTO.getEnLinkUrl());
            bannerDO.setVisible(bannerDTO.getVisible());
        }
        return bannerDO;
    }

    private BannerDTO convertDOToDTO(BannerDO bannerDO) {
        BannerDTO bannerDTO = null;
        if (bannerDO != null) {
            bannerDTO = new BannerDTO();
            bannerDTO.setId(bannerDO.getId());
            bannerDTO.setVisible(bannerDO.getVisible());
            bannerDTO.setUrl(bannerDO.getUrl());
            bannerDTO.setEnUrl(bannerDO.getEnUrl());
            bannerDTO.setTitle(bannerDO.getTitle());
            bannerDTO.setEnTitle(bannerDO.getEnTitle());
            bannerDTO.setLinkUrl(bannerDO.getLinkUrl());
            bannerDTO.setEnLinkUrl(bannerDO.getEnLinkUrl());
            bannerDTO.setId(bannerDO.getId());
        }
        return bannerDTO;
    }

    private BannerQueryDO convertSqlDTOToDO(BannerQueryDTO bannerQueryDTO) {
        BannerQueryDO bannerQueryDO = null;
        if (bannerQueryDTO != null) {
            bannerQueryDO = new BannerQueryDO();
            bannerQueryDO.setVisible(bannerQueryDTO.getVisible());
        }
        return bannerQueryDO;
    }

    private BannerQueryDTO convertSqlDOToDTO(List list) {
        BannerQueryDTO bannerQueryDTO = null;
        bannerQueryDTO = new BannerQueryDTO();
        bannerQueryDTO.setList(list);
        return bannerQueryDTO;
    }
}
